import java.io.IOException;
import java.util.HashSet;

public class TeamParser extends Parser
{

	public TeamParser(String file) throws IOException
	{
		super(file);
	}

	@Override
	public void parse() throws IOException
	{
        String line;
        HashSet<String> tids = new HashSet<String>();
        while ((line = reader.readLine()) != null)
        {
            String[] fields = line.split(",");
            
            String id = fields[1];
            String name = fields[4].replace("'", "''") + " " + fields[5].replace("'", "''");
            String since = "TO_DATE('" + fields[7] + "', 'mm/dd/yyyy')";
            String until = fields[8].isEmpty() ? "NULL" : ("TO_DATE('" + fields[8] + "', 'mm/dd/yyyy')");
            
            tids.add(id);
            
            writer.write("INSERT INTO Team_periods (tid, name, since, until) VALUES ('" 
            		+ id + "', '" + name + "', " + since + ", " + until + ");\n");
        }
        
        writer.write("\n");
        
        for (String tid : tids)
        	writer.write("INSERT INTO Teams (tid) VALUES ('" + tid + "');\n");
        
        reader.close();
        writer.close();
	}

}
